$(function() {
	//验证用户输入的工资范围
	$("#salFrom-in-searchForm").blur(function() {
		closeAlert();
		var salFrom = $("#salFrom-in-searchForm");
		var salTo = $("#salTo-in-searchForm");
		var regExp = /^[0-9]+.?[0-9]*$/;
		if (salFrom.val() == "") {
			salFrom.val("*");
		} else if (!regExp.test(salFrom.val())) {
			showValidateMsg($("#salFrom-in-searchForm"), "请输入数字", false);
			//alertInfo($(".alertContainer"),"工资区间只能输入数字");
			return;
		} else if (regExp.test(salTo.val())) {
			var salFromValue = parseFloat(salFrom.val());
			var salToValue = parseFloat(salTo.val());
			if (salFromValue > salToValue) {
				showValidateMsg($("#salFrom-in-searchForm"), "工资区间不合法", false);
				//alertInfo($(".alertContainer"),"工资区间不合法");
				return;
			}
		}
		showValidateMsg($("#salFrom-in-searchForm"), "", true);
		showValidateMsg($("#salTo-in-searchForm"), "", true);
	});
	$("#salTo-in-searchForm").blur(function() {
		closeAlert();
		var salFrom = $("#salFrom-in-searchForm");
		var salTo = $("#salTo-in-searchForm");
		var regExp = /^[0-9]+.?[0-9]*$/;
		if (salTo.val() == "") {
			salTo.val("*");
		} else if (!regExp.test(salTo.val())) {
			showValidateMsg($("#salTo-in-searchForm"), "请输入数字", false);
			//alertInfo($(".alertContainer"),"工资区间只能输入数字");
			return;
		} else if (regExp.test(salFrom.val())) {
			var salFromValue = parseFloat(salFrom.val());
			var salToValue = parseFloat(salTo.val());
			if (salFromValue > salToValue) {
				showValidateMsg($("#salFrom-in-searchForm"), "工资区间不合法", false);
				//alertInfo($(".alertContainer"),"工资区间不合法");
				return;
			}
		}
		showValidateMsg($("#salFrom-in-searchForm"), "", true);
		showValidateMsg($("#salTo-in-searchForm"), "", true);
	});
	
	//用户点击重置按钮
	$("#resetButton").click(function() {
		$('#searchForm')[0].reset();
		showValidateMsg($("#salFrom-in-searchForm"), "", true);
		showValidateMsg($("#salTo-in-searchForm"), "", true);
	});
	
	//用户点击搜索按钮
	$("#searchButton").click(function() {
		if ($("#searchForm input.error").length == 0) {
			closeAlert();
			var depName = $("#depName-in-searchForm").val();
			var empNo = $("#empNo-in-searchForm").val();
			
			search(depName, empNo, true);
		}
	});
	
	//发送异步搜索请求
	var search = function(depName, empNo, refresh) {
		$.post("fake/fixed-salary-management/table.JSON", 
				{"depName":depName, "empNo":empNo},
				function(response) {
					var jsonData = eval('('+response+')');
					//var jsonData = eval(response);
					if(jsonData.totalRowNumber == 0){
						alertInfo($(".alertContainer"),"没有相关固定工资信息");
						$(".searchResultContainer").html("");
					} else {
						if(refresh){
							closeAlert();
						}
						loadSearchResult(response);
					}
					
					$(".editButton").click(function() {
						closeAlert();
						var empId = $(this).attr("rowId");
						$("#editModal").modal('show'); 
						loadEditModel(empId, response);
					});
				}
		);
	};
	
	//显示搜索表格
	var loadSearchResult = function(searchResult) {
		$(".tableHead").html("");
		$(".tableRow").html("");
		
		var jsonData = eval('('+searchResult+')');
		//var jsonData = eval(searchResult);
		
		//加载表头
		$(".tableHead").append("<th>部门名</th>");
		$(".tableHead").append("<th>员工编号</th>");
		$(".tableHead").append("<th>员工姓名</th>");
		$(".tableHead").append("<th>月份</th>");
		var fixedSalList = jsonData.tableHead.fixedSal;
		for (var i = 0; i < fixedSalList.length; i++) {
			$(".tableHead").append("<th>" + fixedSalList[i] + "</th>");
		}
		$(".tableHead").append("<th></th>");
		
		//加载行
		var tableRow = jsonData.tableRow;
		for (var i = 0; i < tableRow.length; i++) {
			var row = "<tr id='" + tableRow[i].empId + "'>" + "<td>" + tableRow[i].depName +"</td>" + 
					  "<td>" + tableRow[i].empNo +"</td>" + "<td>" + tableRow[i].empName +"</td>" + 
					  "<td>" + tableRow[i].month +"</td>";
			var salaryList = tableRow[i].fixedSal;
			for (var j = 0; j < salaryList.length; j++) {
				row += "<td>" + salaryList[j] +"</td>"
			}
			row += "</tr>";
			$(".tableRow").append(row);
			$("#" + tableRow[i].empId).append("<td><div class='hidden-sm hidden-xs btn-group'><button class='editButton btn btn-xs btn-info'" +
											  "rowId='" + tableRow[i].empId + "'>" + 
											  "<i class='ace-icon fa fa-pencil bigger-120'></i></button></div></td>");
		}
	};
	
	//显示修改Modal
	var loadEditModel = function(empId, response) {
		var jsonData = eval('('+response+')');
		//var jsonData = eval(response);
		
		$(".fixedSalRow").remove();
		
		var tableHead = jsonData.tableHead;
		var fixedSalName = tableHead.fixedSal;
		var rowNum = 0;
		for (var i = 0; i < fixedSalName.length; i++) {
			if (i % 2 == 0) {
				var fixedSal = "<div id='fixedSalRow" + rowNum + "-in-editModal' class='fixedSalRow row'>";
				fixedSal += "<div class='col-xs-12 col-sm-6 col-md-5 col-md-push-1'>" +
							 "<div class='space space-4'></div><label>" + fixedSalName[i] + "</label>" +
							 "<input id='fixedSal" + i + "-in-editModal' type='text' " +
							 "class='form-control fixedSalInput'/><div class='space space-4'></div></div>";
				$("#editModalFormBody").append(fixedSal);
			} else {
				var fixedSal = "<div class='col-xs-12 col-sm-6 col-md-5 col-md-push-1'>" +
							   "<div class='space space-4'></div><label>" + fixedSalName[i] + "</label>" +
							   "<input id='fixedSal" + i + "-in-editModal' type='text' " +
							   "class='form-control fixedSalInput'/><div class='space space-4'></div></div>";
				$("#fixedSalRow" + rowNum + "-in-editModal").append(fixedSal);
				rowNum++;
			}
		}
		
		var tableRow = jsonData.tableRow;
		var i = 0;
		while (tableRow[i].empId != empId) {
			i++;
		}
		var row = tableRow[i];
		$("#depName-in-editModal").val(row.depName);
		$("#empNo-in-editModal").val(row.empNo);
		$("#empName-in-editModal").val(row.empName);
		$("#month-in-editModal").val(row.month);
		for (var i = 0; i < row.fixedSal.length; i++) {
			$("#fixedSal" + i + "-in-editModal").val(row.fixedSal[i]);
		}
		
		$(".fixedSalInput").blur(function() {
			salInput = $(this);
			for (var i = 0; i < salInput.length; i++) {
				validateNumberInput(salInput.eq(i));
			}
		});
	};
	
	//用户确认修改
	$("#confirmButton-in-editModal").click(function() {
		if ($("#editModalForm input.error").length == 0) {
			$("#editModal").modal('hide');
			
			//$.post("", {}, function(response) {});
			search(null, null, true);
		}
	});
	
	//用户点击录入按钮
	$("#importButton").click(function() {
		$.post("fake/fixed-salary-management/table.JSON", 
				{},
				function(response) {
					closeAlert();
					
					//显示录入Modal
					$("#importModal").modal('show');
					
					var jsonData = eval('('+response+')');
					//var jsonData = eval(response);
					
					$(".fixedSalRow").remove();
					$("#depName-in-importModal").val("");
					$("#posName-in-importModal").val("");
					$("#month-in-importModal").val("");
					delValidateMsg($("#depName-in-importModal"));
					delValidateMsg($("#posName-in-importModal"));
					delValidateMsg($("#month-in-importModal"));
					
					var tableHead = jsonData.tableHead;
					var fixedSalName = tableHead.fixedSal;
					var rowNum = 0;
					for (var i = 0; i < fixedSalName.length; i++) {
						if (i % 2 == 0) {
							var fixedSal = "<div id='fixedSalRow" + rowNum + "-in-importModal' class='fixedSalRow row'>";
							fixedSal += "<div class='col-xs-12 col-sm-6 col-md-5 col-md-push-1'>" +
										 "<div class='space space-4'></div><label>" + fixedSalName[i] + "</label>" +
										 "<input id='fixedSal" + i + "-in-importModal' type='text' " +
										 "class='form-control fixedSalInput'/><div class='space space-4'></div></div>";
							$("#importModalFormBody").append(fixedSal);
						} else {
							var fixedSal = "<div class='col-xs-12 col-sm-6 col-md-5 col-md-push-1'>" +
										   "<div class='space space-4'></div><label>" + fixedSalName[i] + "</label>" +
										   "<input id='fixedSal" + i + "-in-importModal' type='text' " +
										   "class='form-control fixedSalInput'/><div class='space space-4'></div></div>";
							$("#fixedSalRow" + rowNum + "-in-importModal").append(fixedSal);
							rowNum++;
						}
					}
					
					$(".fixedSalInput").blur(function() {
						salInput = $(this);
						for (var i = 0; i < salInput.length; i++) {
							validateNumberInput(salInput.eq(i));
						}
					});
				}
		); 
	});
	
	//验证录入表单
//	$("#importModalForm").validate({
//		rules: {
//			depName: "required",
//			posName: "required",
//			importDate: "required"
//		},
//		messages: {
//			depName: "请选择部门",
//			posName: "请选择岗位",
//			importDate: "请选择日期"
//		}
//	});
	
	$(".importInput").change(function() {
		importInput = $(this);
		for (var i = 0; i < importInput.length; i++) {
			validateRequiredInput(importInput.eq(i));
		}
	});
	
	//用户确认录入
	$("#confirmButton-in-importModal").click(function() {
		validateRequiredInput($("#depName-in-importModal"));
		validateRequiredInput($("#posName-in-importModal"));
		validateRequiredInput($("#month-in-importModal"));
		if ($("#importModalForm input.error").length == 0) {
			$("#importModal").modal('hide');
			
			//$.post("", {}, function(response) {});
		}
	});
	
	//验证是否填写必填项
	var validateRequiredInput = function(element) {
		if(element.val() != "") {
			showValidateMsg(element, "请选择", true);
		} else {
			showValidateMsg(element, "请选择", false);
		}
	};
	
	//验证输入是否为数字
	var validateNumberInput = function(element) {
		var regExp = /^[0-9]+.?[0-9]*$/;
		if(regExp.test(element.val())) {
			showValidateMsg(element, "请输入数字", true);
		} else {
			showValidateMsg(element, "请输入数字", false);
		}
	};
	
	//显示表单验证信息
	var showValidateMsg = function(element, msg, isValid) {
		if(!isValid) {
			element.attr("class", "form-control error");
			element.attr("aria-invalid", true);
			if ($("#" + element.attr("id") + "-error").length > 0) {
				$("#" + element.attr("id") + "-error").html(msg);
				$("#" + element.attr("id") + "-error").css("display", "inline-block");
			} else {
				var errorLabel = "<label id='" + element.attr("id") + "-error' class='error' for='" + element.attr("id") + "' style=''>" +
								 msg + "</label>";
				element.after(errorLabel);
			}
		} else {
			element.attr("class", "form-control valid");
			element.attr("aria-invalid", false);
			$("#" + element.attr("id") + "-error").css("display", "none");
		}
	};
	
	//删除表单验证信息
	var delValidateMsg = function(element) {
		$("#" + element.attr("id") + "-error").remove();
	};
	
	var alertInfo = function(container, msg) {
		container.css("display","none");
		container.html("");
		var div = "<div class='alert_div alert alert-warning alert-dismissible fade in' role='alert'>" +
				  "<button type='button' id='closeAlert' class='close' data-dismiss='alert' aria-label='Close'>" +
				  "<span aria-hidden='true'>×</span>" + "</button>" + "<strong class='show_ms'></strong>" +
				  "</div>";
		container.html(div);
		$(".show_ms").text(msg);
		container.css("display","");
	};
	
	var closeAlert = function() {
		
		$(".alertContainer").html("");
		$(".alertContainer").css("display","none");
	};
	
});


